//
//  ZDController.m
//  特种车调度
//
//  Created by 陈宇 on 15/8/18.
//
//

#import "ZDController.h"
#import "ZDCell.h"
#import "BMAccountManager.h"
#import <BaiduMapAPI/BMapKit.h>

@interface ZDController () <UITableViewDataSource, UITableViewDelegate>

#pragma mark - 列表
@property (weak, nonatomic) IBOutlet UIView *listContainer;
@property (weak, nonatomic) IBOutlet UITextField *keywordsTextField;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;
@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *stations;

#pragma mark - 详细
@property (weak, nonatomic) IBOutlet UIView *detailContainer;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;

- (IBAction)searchAction:(UIButton *)sender;
- (IBAction)okAction:(UIButton *)sender;

@end

@implementation ZDController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的站点";
    self.navigationItem.leftBarButtonItem.target = self;
    
    if (!_edit) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStylePlain target:self action:@selector(updateStation)];
    }
    
    [self.okButton setBackgroundImage:[UIImage imageWithColor:kGlobalColor] forState:UIControlStateNormal];
    self.okButton.layer.cornerRadius = 20.f;
    self.okButton.layer.masksToBounds = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_edit) {
        [self getList];
    } else {
        [self getData];
    }
}

- (void)updateStation
{
    ZDController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"zdVC"];
    vc.edit = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)back
{
    if (_type == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)getData
{
    BMRequest *request = [BMRequest requestWithPath:@"getMyStationByDispatcherId.htm" contentKey:@"myStation"];
    request.params = @{@"id": @([BMAccountManager sharedInstance].user.ID)};
    [self showHudWithhint:kDefaultMessage];
    [ZDStation postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            [self setStation:response.result];
        } else if (response.status == 2) {  //代表用户没有选择站点。
            [self getList];
        } else {
            [self showHint:@"获取站点信息失败!"];
        }
    }];
}

- (void)getList
{
    weakSelf(self);
    self.listContainer.hidden = NO;
    
    NSString *name = _keywordsTextField.text;
    BMRequest *request = [BMRequest requestWithPath:@"getAllStations.htm" contentKey:@"stations"];
    request.params = @{@"name": name ? name : @""};
    [self showHudWithhint:kDefaultMessage];
    [ZDStation postWithRequest:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            weakSelf.stations = response.result;
            [self.tableView reloadData];
        } else {
            [self showHint:@"获取站点列表信息失败!"];
        }
    }];
}

- (void)setStation:(ZDStation *)statioin
{
    self.detailContainer.hidden = NO;
    self.titleLabel.text = statioin.name;
    self.detailLabel.text = statioin.addAddress;
    BMKPointAnnotation *anno = [[BMKPointAnnotation alloc] init];
    anno.title = statioin.name;
    anno.subtitle = statioin.addAddress;
    anno.coordinate = CLLocationCoordinate2DMake(statioin.latitude, statioin.longitude);
    [_mapView addAnnotation:anno];
    [_mapView setRegion:BMKCoordinateRegionMake(anno.coordinate, BMKCoordinateSpanMake(0.02, 0.02))];
    //    [_mapView setCenterCoordinate:anno.coordinate animated:YES];
    [_mapView setZoomLevel:15];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _stations.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.station = _stations[indexPath.row];
    return cell;
}

- (IBAction)searchAction:(UIButton *)sender {
    [self getList];
}

- (IBAction)okAction:(UIButton *)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    if (!indexPath) {
        [self showHint:@"您还没有选择任何站点!"];
        return;
    }
    
    BMRequest *request = [BMRequest requestWithPath:@"chooseStation.htm"];
    request.params = @{@"stationId": @([_stations[indexPath.row] ID]), @"id": @([BMAccountManager sharedInstance].user.ID)};
    
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            self.listContainer.hidden = YES;
            if (_edit) {
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [self getData];
            }
        } else {
            [self showHint:@"选择站点成功!"];
        }
    }];
}

@end
